package ar.edu.unlu.sistemas.p2p.business.msg;

import java.net.InetSocketAddress;

import org.jdom.Element;

import ar.edu.unlu.sistemas.p2p.business.util.StringUtils;
import ar.edu.unlu.sistemas.p2p.business.vo.QueryResultVO;

public class QueryResult {
	
	private String address;
	private String file;
	private Float rating;
	
	public static final String TAG_ROOT = "result";
	private static final String TAG_ADDR = "address";
	private static final String TAG_FILE = "file";
	private static final String TAG_RATING = "rating";
	
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getFile() {
		return file;
	}
	public void setFile(String file) {
		this.file = file;
	}
	public Float getRating() {
		return rating;
	}
	public void setRating(Float rating) {
		this.rating = rating;
	}
	
	public QueryResult() {
		
	}
	
	public QueryResult(QueryResultVO vo) {
		this.address = vo.getAddress().getAddress().getHostAddress()+":"+String.valueOf(vo.getAddress().getPort());
		this.file = vo.getDocName();
		this.rating = vo.getValue();
	}
	
	public QueryResult(String address, String file, Float rating) {
		this.address = address;
		this.file = file;
		this.rating = rating;
	}
	
	public QueryResult(InetSocketAddress address, String file, Float rating) {
		this.address = address.getAddress().getHostAddress().toString()+":"+String.valueOf(address.getPort());
		this.file = file;
		this.rating = rating;
	}
	
	public Element toXMLElement() {
		Element el = new Element(TAG_ROOT);
		el.addContent(new Element(TAG_ADDR).setText(this.getAddress()));
		el.addContent(new Element(TAG_FILE).setText(this.getFile()));
		el.addContent(new Element(TAG_RATING).setText(StringUtils.fromFloatToString(this.getRating())));
		return el;
	}
	
	public static QueryResult buildFromXMLElement(Element headerElement) {
		QueryResult result =new QueryResult();
		result.setAddress(headerElement.getChild(TAG_ADDR).getText());
		result.setFile(headerElement.getChild(TAG_FILE).getText());
		result.setRating(new Float(headerElement.getChild(TAG_RATING).getText()));
		return result;
	}
	
	public QueryResultVO getVO() {
		return new QueryResultVO(this.getAddress(),this.getFile(),this.getRating());
	}

}
